import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { Address } from '../types/address';

interface AddressState {
    selectedAddress: Address | null;
}

const initialState: AddressState = {
    selectedAddress: null
};

export const addressSlice = createSlice({
    name: 'address',
    initialState,
    reducers: {
        // 选择收货地址
        selectAddress: (state, action: PayloadAction<Address>) => {
            state.selectedAddress = action.payload;
            // console.log(state.selectedAddress);
        },
        // 清除选中的地址
        clearAddress: (state) => {
            state.selectedAddress = null;
        }
    }
});

export const { selectAddress, clearAddress } = addressSlice.actions;
export default addressSlice.reducer;
